from django.contrib.auth.base_user import AbstractBaseUser
from django.contrib.auth.models import PermissionsMixin, UserManager
from django.contrib.auth.validators import UnicodeUsernameValidator
from django.db import models
from django.utils import timezone

from tag.models import Tag

GENDER_CHOICES = (
    ('Male', 'Male'),
    ('Female', 'Female'),
    ('Other', 'Other')
)


class User(AbstractBaseUser, PermissionsMixin):
    username_validator = UnicodeUsernameValidator()
    objects = UserManager()
    USERNAME_FIELD = 'phone'
    REQUIRED_FIELDS = ['username', ]

    # ---------------- field ---------------
    email = models.CharField(max_length=100, blank=True, null=True)
    is_staff = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    date_joined = models.DateTimeField(default=timezone.now)

    username = models.CharField(max_length=100, blank=True, null=True)
    phone = models.CharField(max_length=20, unique=True)
    gender = models.CharField(choices=GENDER_CHOICES, max_length=6, default='Other')
    is_deleted = models.BooleanField(default=False)
    created_time = models.DateTimeField(auto_now_add=True)
    portrait = models.CharField(max_length=200, blank=True)
    signature = models.CharField(max_length=400, blank=True,
                                 default='和快知一起分享你的知识')
    expertise_area = models.ForeignKey(Tag, related_name='user',
                                       on_delete=models.CASCADE, blank=True,null=True)

    qiniu_token = models.CharField(max_length=100, null=True, blank=True)

    def __str__(self):
        return self.phone

    class Meta:
        db_table = u'account_db'


class FocusUser(models.Model):
    """用户关注"""
    creator = models.ForeignKey(User, related_name='focus_user', on_delete=models.CASCADE)
    focused = models.ForeignKey(User, related_name='follower', on_delete=models.CASCADE)

    created_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        unique_together = (("creator", "focused"),)
